package com.tansun.easycare.rule.table.exclusions.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.table.exclusions.entity.ExclusionsBO;
import com.tansun.easycare.rule.table.exclusions.entity.ExclusionsVo;
import com.tansun.easycare.rule.table.exclusions.service.ExclusionsService;
import com.tansun.easycare.rule.utils.JsonUtil;
import com.tansun.rule.common.RuleModelTypeEnum;

/**
 * 排除项 Action 20160825
 *
 * @author linmin
 */
@Controller
@RequestMapping("/ruleProj/exclusions")
public class ExclusionsController extends RuleBaseController {
    @Autowired
    private ExclusionsService exclusionsService;
    @Autowired
    private RuleManageService ruleManageService;

    /**
     * 打开排除项主页面
     *
     * @param exclusionsModel 排除项模型，用于过滤排除项
     * @param modelMap        传递相关数据到结果页面
     * @return 排除项列表主页面
     */
    @RequestMapping("index")
    public String index(HttpServletRequest request, HttpServletResponse response, RuleManageVO param, ModelMap modelMap) throws Exception {
        param.setProjectCode(this.getCurrProjectCode());
        param.setModelType(RuleModelTypeEnum.Exclusions.getCode());
        PageInfo<RuleManageVO> page = this.ruleManageService.findRuleManageVO(param.getPage(), param);
        modelMap.put("page", page);
        modelMap.put("projectCode", this.getCurrProjectCode());
        modelMap.put("condition", JsonUtil.object2json(param));
        return "rule/table/exclusions/index";
    }

    /**
     * 打开排除项详细信息
     *
     * @param ModelMap 传递相关数据到结果页面
     * @param editable 是否可编辑
     * @param code     排除项的 code
     * @return 排除项详细信息页面
     */
    @RequestMapping("openExclusions")
    public String openExclusions(ModelMap modelMap, boolean isEditable, String indexId) throws Exception {
        RuleManage rulemanage = new RuleManage();
        if (!StringUtils.isEmpty(indexId)) {
            rulemanage = this.ruleManageService.getById(indexId);
        }
        modelMap.put("exclusionsModel", rulemanage);
        modelMap.put("projectCode", this.getCurrProjectCode());
        modelMap.put("isEditable", isEditable);
        return "rule/table/exclusions/detail";
    }

    /**
     * 保存或更新排除项
     *
     * @param exclusionsVo 排除项视图，包含排除项模型和规则列表
     * @return 处理结果信息
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonMessage saveOrUpdate(@RequestBody ExclusionsVo exclusionsVo) throws Exception {
        JsonMessage result = new JsonMessage();
        result.setResult(true);
        try {
            exclusionsService.saveOrUpdate(exclusionsVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 通过 code 获取排除项详细信息
     *
     * @param code    排除项的 code
     * @param version 排除项的版本
     * @return JSON 格式的排除项信息
     */
    @RequestMapping("getExclusionsDetailByCode")
    public @ResponseBody
    JsonMessage getExclusionsDetailByCode(HttpServletRequest request, String indexId, String version) throws Exception {
        JsonMessage result = new JsonMessage();
        try {

            RuleManage indexModel = this.ruleManageService.getById(indexId);
            ExclusionsVo exclusionsVo = new ExclusionsVo();
            exclusionsVo.setExclusionsModel(indexModel);
            if (StringUtils.isEmpty(version))
                version = indexModel.getVersion();
            exclusionsVo.setExclusionsList(this.exclusionsService.findExclusionItemByIndexId(indexId, version));
            result.setData(exclusionsVo);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 获取排除项版本列表信息
     *
     * @param code        排除项的 code
     * @param currVersion 当前的版本，用于过滤当前版本
     * @return JSON 格式的版本信息
     */
    @RequestMapping("exclusionsVersions")
    @ResponseBody
    public JsonMessage exclusionsVersions(String code, String currVersion) throws Exception {
        JsonMessage result = new JsonMessage();
        try {
            // 获取排除项版本信息
//			List<VersionModel> versionList = exclusionsService.getExclusionsVersions(code, currVersion);
//			
//			result.setResult(true);
//			result.setData((Serializable)versionList);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 归档指定版本及之前的的排除项
     *
     * @param code    排除项的 code
     * @param version 排除项版本
     * @return 处理结果信息
     */
    @RequestMapping("archiveFile")
    @ResponseBody
    public JsonMessage archiveFile(String code, String targetVersion) {
        JsonMessage result = new JsonMessage();
        try {
//			exclusionsService.archiveFile(code, targetVersion);

            result.setResult(true);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 打开版本比对页面
     *
     * @param modelMap       传递相关数据到结果页面
     * @param code           排除项的 code
     * @param baseVersion    基础版本，被比对的版本
     * @param compareVersion 比对版本
     * @return 版本比较详细信息页面
     */
    @RequestMapping("openVersionCompare")
    public String openVersionCompare(ModelMap modelMap, String code, String baseVersion, String compareVersion) throws Exception {
        modelMap.put("code", code);
        modelMap.put("baseVersion", baseVersion);
        modelMap.put("compareVersion", compareVersion);
        modelMap.put("projectCode", this.getCurrProjectCode());

        return "rule/table/exclusions/compare";
    }

    /**
     * 获取版本比较的排除项信息
     *
     * @param code           排除项的 code
     * @param baseVersion    基础版本
     * @param compareVersion 比较版本
     * @return 两个版本的排除项详细信息
     */
    @RequestMapping("compareVersions")
    @ResponseBody
    public JsonMessage compareVersions(String code, String baseVersion, String compareVersion) throws Exception {
        JsonMessage result = new JsonMessage();
        try {
            ExclusionsVo baseVersionVo = new ExclusionsVo();
            ExclusionsVo compareVersionVo = new ExclusionsVo();
            RuleManage ruleManage = this.ruleManageService.get(code);
            baseVersionVo.setExclusionsModel(ruleManage);
            compareVersionVo.setExclusionsModel(ruleManage);

            List<ExclusionsVo> exclusionsVoList = new ArrayList<ExclusionsVo>();
            List<ExclusionsBO> baseList = this.exclusionsService.findExclusionItemByIndexId(baseVersionVo.getExclusionsModel().getId(), baseVersion);
            List<ExclusionsBO> compList = this.exclusionsService.findExclusionItemByIndexId(baseVersionVo.getExclusionsModel().getId(), baseVersion);
            baseVersionVo.setExclusionsList(baseList);
            compareVersionVo.setExclusionsList(compList);
            exclusionsVoList.add(baseVersionVo);
            exclusionsVoList.add(compareVersionVo);

            result.setResult(true);
            result.setData((Serializable) exclusionsVoList);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public void setRuleBizService() {
        super.setRuleBaseService(this.exclusionsService);
    }
}
